/**
  ******************************************************************************
  * @file    pwm.c
  * @author  测试1组（houtiezhu）
  * @version v0
  * @date    09-07-2019
  *           
  * @verbatim
  **/
/* Includes ------------------------------------------------------------------*/

#include "define.h"
#include "PWM.h"



uint8  	BB; 
uint8   InflateValueSet;
uint16 PUMPPWMValue;
uint8  InflateValueSet;
uint16  InflateValue;	
uint8 	InflateRate;
uint8 limit=30;

//PWM配置
void UserPwm1_Init(void)
{
	PWM_InitTypeDef PWM_PDM_InitStruct;
	RCC_APBPeriphClockCmd(RCC_APBPeriph_PWM1,ENABLE);  	
	RCC_APB2PeriphClockConfig(RCC_APBPeriph_PWM1,RCC_PWMCLK_IHRC,RCC_PWMCLK_DIV4);

	PWM_PDM_InitStruct.PWM_WaveMode = DISABLE;//ENABLE
	PWM_PDM_InitStruct.PWM_PDM_Mode = PWM_Mode;
	PWM_PDM_InitStruct.PWM_Period = 255;//20K   
	PWM_PDM_InitStruct.PWM_DutyCycle = 150;
	PWM_PDM_Init(PWM1,&PWM_PDM_InitStruct);
	
	PWM_ITCmd(PWM1,DISABLE);//DISABLE
	//PWM_PDM_Cmd(PWM1,ENABLE);
	PWM_PDM_Cmd(PWM1,DISABLE);
}


void ValvePwm(void)
{
	#if 0
	uint8 firstdecrese=0;
	uint8  DeflateTemp;
	uint16 Temp;
	DeflateSetting=55;
	
	if(!LinearValveFlag)
	{	
		VALVEPWMValue=65535;
		return;
	}
	
  if (T625ms == 0) OneShotValve= 0;              
	else
		{
			if (OneShotValve == 0)
				{
					OneShotValve=1;	
					
					DeflateValueBack[3]=DeflateValueBack[2];
					DeflateValueBack[2]=DeflateValueBack[1];
					DeflateValueBack[1]=DeflateValueBack[0];
					DeflateValueBack[0]=DeflateValue;
					DeflateTemp=DeflateValueBack[2];
	//**************************************************************************************
					if(DeflateValue>=DeflateSetting)														//比目标值大
						{
							
							DeflateDiff2=DeflateValue-DeflateSetting;
							if(DeflateValue>DeflateTemp)									//比第三笔漏速	 
								{
									DeflateDiff=DeflateValue-DeflateTemp;    	//表示上升；大于设定值且上升，即减少DeflateValueSet；
									
									DeflateAdjus=((DeflateDiff2/25)+DeflateDiff/5);
								}
							else															
								{
									DeflateDiff=DeflateTemp-DeflateValue;			//表示下降；大于设定值且下降，即减少DeflateValueSet；
									if((DeflateDiff2/25)>DeflateDiff/5)
										{
											DeflateAdjus=((DeflateDiff2/25)-DeflateDiff/5);
										}
									else DeflateAdjus=1;
								}
								
								if(PressuremmHgw<SetPressureVw-10)									//前面调整后，后面调整要小一些。
									{
										if(DeflateAdjus>4) DeflateAdjus=4;
									}
								if(DeflateAdjus>6) DeflateAdjus=6;

								DeflateAdjus/=2;	
								DeflateValueSet+=DeflateAdjus;
						}
					else																										//比目标值小																		
						{
							DeflateDiff2=DeflateSetting-DeflateValue;
							if(DeflateValue<DeflateTemp)												//比第三笔漏速	 
								{
									DeflateDiff=DeflateTemp-DeflateValue;						//表示下降；小于设定值且下降，即增加DeflateValueSet；
									DeflateAdjus=((DeflateDiff2/6)+DeflateDiff/2);
								}
							else
								{
									DeflateDiff=DeflateValue-DeflateTemp;    				//表示上升；小于设定值且上升，即增加DeflateValueSet；
									if((DeflateDiff2/6)>DeflateDiff/5)
										{
											DeflateAdjus=((DeflateDiff2/6)-DeflateDiff/5);
										}
									else DeflateAdjus=0;
								}
								
							if(PressuremmHgw>=SetPressureVw-5)	{if(DeflateAdjus>8) DeflateAdjus=8;}
							else if(PressuremmHgw>=SetPressureVw-15)	{if(DeflateAdjus>4) DeflateAdjus=4;}
							else {if(DeflateAdjus>2) DeflateAdjus=2;}
							
							if(DeflateValueBack[0]<20&&DeflateValueBack[1]<20&&
								DeflateValueBack[2]<20&&DeflateValueBack[3]<20)
							{
								DeflateAdjus*=2;
								if(DeflateAdjus>8) DeflateAdjus=8;
							}
							
							DeflateValueSet-=DeflateAdjus;
						}
					
				}
		}

		if(first1flag==0)
		{
			noactioncount=0;
			first1flag=1;
			
			if(SetPressureVw>160)
			{
				Temp=SetPressureVw-160;
				Temp/=5;
			}
			else 
			{
				Temp=0;
			}
			if(Temp>24) Temp=24;
			if(SetPressureVw>210) Temp+=20;
			DeflateValueSet=1100+Temp;
			
			if(BatValue<620)
			{
				if(BatValue<400) BatValue=400;
				z=620-BatValue;
				
				if(SetPressureVw>160)
				{
					y=SetPressureVw-160;
				}
				else y=0;
				
				x=200;
				x*=z;
				x*=y;
				x/=220;
				x/=120;
				DeflateValueSet+=x;
			}

			if(BatValue<620)
			{
				if(BatValue<400) BatValue=400;
				Temp=620-BatValue;
				
				Temp*=14;
				Temp/=7;
				DeflateValueSet+=Temp;
			}
			
			if(SetPressureVw<170) DeflateValueSet-=10;
			
			FirstDeflate=0;
			DeflateCount=0;
			
			///*if(VavleTestFlag)*/ DeflateValueSet-=250;
			DeflateValueSet+=valveoffset;
		}		
						
		if(DeflateValueSet<400){DeflateValueSet=400;}
		if(DeflateValueSet>1800){DeflateValueSet=1800;}	
		
		VALVEPWMValue=65536-2048+DeflateValueSet;
		
		/*
		DisplayToMemorySet(DeflateValue);
		DisplayHalfByte(DeflateValueSet/1000,PulseTen);
		DisplayHalfByte(DeflateValueSet%1000/100,PulseSingle);
		DisplayHalfByte(DeflateValueSet%100/10,SYSTen);
		DisplayHalfByte(DeflateValueSet%10,SYSSingle);*/
#endif
}


void PumpPwm(void)
{ 
#if 1
	uint8 x;
  if (T125ms == 0) OneShotPump= 0;              //20120717
	else
		{
			if (OneShotPump == 0)
				{
					OneShotPump=1;		

					if(InflateValue>InflateRate)
					{
						x=InflateValue-InflateRate;
						if(x>10) InflateValueSet-=1;
						if(x>25) InflateValueSet-=1;
						if(x>40) InflateValueSet-=1;
					}
					else
					{
						x=InflateRate-InflateValue;
						if(x>10) InflateValueSet+=1;
						if(x>25) InflateValueSet+=1;
						if(x>40) InflateValueSet+=1;
					}
	
					if(PressuremmHgw<limit)
						{	
							if(UparmWWFlag){InflateValueSet=180;}
							else{InflateValueSet=240;}		//20121002
							First30mmHg=0;
						}
					if((PressuremmHgw>=limit)&&(!First30mmHg))
						{
							if(UparmWWFlag){InflateValueSet=160;}
							else{InflateValueSet=240;}		//20121002
							
							if(InfDefFlag) InflateValueSet=130;
							
							First30mmHg=1;
						}
					if(InflateValueSet<80){InflateValueSet=80;}
					if(InflateValueSet>250){InflateValueSet=250;}	
					//PUMPPWMValue=65536-256+InflateValueSet;
					PUMPPWMValue=InflateValueSet;
					PWM_SetDutyCycle(PWM1 ,PUMPPWMValue );
				}
		}
//	PUMPPWMValue=65536-256+150;
#endif
	/*PWM_SetDutyCycle(PWM1 ,10 );
	delay(10);
	PWM_SetDutyCycle(PWM1 ,20 );
	delay(10);
	PWM_SetDutyCycle(PWM1 ,100 );
	delay(10);*/
}

/************************************************/
void iocontrolon(void) 
{
	if( BB >=0x80)
  	{
   		BB -=0x80;
  	}
  if( BB >=0x40)
  	{
  		BB -=0x40;
  		//OUT_POWER=0;
   	}	
  if( BB >=0x20)
  	{
  		BB -=0x20;
		InflateValueSet=165;
  		PUMPPWM_EN;
   	}	 
  if( BB >=0x10)
  	{
			if(RCSensorType&&LinearValveFlag) 	
				{
					//VALVEPWMValue=65536-256+255;
					VALVEPWM_EN;
				}
			else
				{
					//OUT_VALVE=1;
					OUT_VALVE(1);
				}
   	}		
}
/************************************************/
void iocontroloff(void)
{
	if (BB >= 0x80)
	{
		BB -=0x80;	
	}
	if( BB >=0x40)
	{
		//OUT_POWER=1;
		BB -=0x40;
	}
	if( BB >=0x20)
	{
		BB -=0x20;
		PUMPPWM_DIS;
	}
	if( BB >=0x10)
	{
		if(RCSensorType&&LinearValveFlag) 	
			{
				VALVEPWM_DIS;
			}
		else
			{
				//OUT_VALVE=0;
				OUT_VALVE(0);
			}
	}	
}



void PumpUp(uint8 speed)
{
	//PUMPPWMValue=65280+speed;
	PUMPPWMValue=speed;
	PUMPPWM_EN;
}
void PumpOff(void)
{
	PUMPPWM_DIS;
}
























